﻿#ifndef SERIALPORTOBJ_H
#define SERIALPORTOBJ_H

#include <QDateTime>
#include <QDebug>
#include <QMutexLocker>
#include <QObject>
#include <QSerialPort>
#include <QThread>
#include <QTimer>

class SerialPortObj : public QObject {
    Q_OBJECT
public:
    explicit SerialPortObj( const QString& deviceName, const QString& portName, int portBaud, const QSerialPort::Parity parity = QSerialPort::NoParity, QObject* parent = nullptr );

    ~SerialPortObj();
    void init( bool isImmediateRead, int msec = 18 );  //默认延时接收18毫秒数据
    bool         isOpened() const;
    bool         isConnected() const;
    QSerialPort* SerialPort() const;

    QString deviceName() const;
    void setMsec( int msec );

private:
    QSerialPort*        serial_port_;
    QString             port_name_;
    QString             device_name_;
    int                 baud_rate_;
    QSerialPort::Parity parity_;
    bool                is_opened_;
    bool                is_connected_;
    QMutex              mutex_;
    int                 delayMsec;
signals:
    void sigReadData( const QByteArray& buffData );

public slots:
    void sltReadData();
    void sltSendData( SerialPortObj* obj, const QByteArray& buffData );

private slots:
    void sltHandleReadyRead();
    void sltImmediateReadyRead();
};

#endif  // SERIALPORTOBJ_H
